<?php

/*
 * 系统设置控制器
 * @author turtle
 * create_time 2017-01-04
 */


namespace App\Admin\Controllers\Sets;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Intervention\Image\Constraint;
use Intervention\Image\Facades\Image;
use App\Admin\Controllers\Controller;
use App\Admin\Models\User;
use App\Admin\Models\Setting;
use App\Admin\Models\Logs;
use App\Admin\Models\UploadImg;

class SetController extends Controller {

    /*
     * 系统设置页面
     * @author turtle
     * create_time 2017-01-04
     */

    public function index() {
        $bool = User::checkUserPermissions('edit_setting'); 
        if (!$bool) {                                                
            return redirect() 
                ->route('admin.dashboard')
                ->with([
                    'message' => "您没有拥有该权限，请联系管理员",
                    'alert-type' => 'error',
                ]);               
        }

        $setting = Setting::first();
        $datas['setting'] = $setting;
        return view('admin.setting', $datas);
    }

    /*
     * 编辑或添加系统设置
     * @author turtle
     * create_time 2017-01-04
     */

    public function edit(Request $request) {
        $bool = User::checkUserPermissions('edit_setting'); 
        if (!$bool) {                                                
            return redirect() 
                ->route('admin.dashboard')
                ->with([
                    'message' => "您没有拥有该权限，请联系管理员",
                    'alert-type' => 'error',
                ]);               
        }
        $setting = Setting::first();
        $data['title'] = $request->input('title');
        $title_logo = $request->file('title_logo');
        $xt_logo = $request->file('xt_logo');
        $bg = $request->file('bg');
        if (!empty($setting)) {

            if (!empty($title_logo)) {
                $data['title_logo'] = UploadImg::upload($title_logo, 'settings');
            }
            if (!empty($xt_logo)) {
                $data['xt_logo'] = UploadImg::upload($xt_logo, 'settings');
            }
            if (!empty($bg)) {
                $data['bg'] = UploadImg::upload($bg, 'settings');
            }
            $data['updated_at'] = date('Y-m-d H:m:s');

            $rs = Setting::where('id', 1)->update($data);
            if ($rs === false) {
                $log = "编辑系统设置失败";
                Logs::addLog($log);
                return redirect()
                    ->route('admin.settings.index')
                    ->with([
                        'message' => "编辑系统设置失败",
                        'alert-type' => 'error',
                    ]);
            } else {
                $log = "编辑系统设置成功";
                Logs::addLog($log);
                return redirect()
                    ->route('admin.settings.index')
                    ->with([
                        'message' => "编辑系统设置成功",
                        'alert-type' => 'success',
                    ]);

            }

        } else {
            $data['title_logo'] = UploadImg::upload($title_logo, 'settings');
            $data['xt_logo'] = UploadImg::upload($xt_logo, 'settings');
            $data['bg'] = UploadImg::upload($bg, 'settings');
            $data['created_at'] = date('Y-m-d H:m:s');
            $data['updated_at'] = date('Y-m-d H:m:s');

            $rs = Setting::insert($data);
            if ($rs) {
                $log = "添加系统设置成功";
                Logs::addLog($log); 
                return redirect()
                    ->route('admin.settings.index')
                    ->with([
                        'message' => "添加系统设置成功",
                        'alert-type' => 'success',
                    ]);

            } else {
                $log = "添加系统设置失败";
                Logs::addLog($log);
                return redirect()
                    ->route('admin.settings.index')
                    ->with([
                        'message' => "添加系统设置失败",
                        'alert-type' => 'error',
                    ]);

            }

        }
    }

}
